package com.xzcedu.jdbc.preparedstatement_;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class exercise01 {
    public static void main(String[] args) throws Exception {
        /**
         * 课堂练习
         * 1. 创建 admin表
         * 2. 使用PreparedStatement 添加5条数据
         * 3. 修改tom的记录，将name 改成 king
         * 4. 删除一条记录
         * 5. 查询全部记录，并显示在控制台
         */
//        Scanner scanner = new Scanner(System.in);
//        //让用户输入管理员名和密码
//        System.out.print("请输入管理员的名字：");   //next(): 当接收到 空格或者 '就表示结束
//        String addmin_name = scanner.nextLine();//说明，如果希望看到SQL注入问题，这里需要用nextLine
//        System.out.print("请输入管理员的密码：");
//        String addmin_pwd = scanner.nextLine(); //nextLine(): 获取一行数据，enter表示结束

        //通过Properties对象获取配置文件的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\com\\xzcedu\\jdbc\\mysql.properties"));
        String user = properties.getProperty("user");
        String passwd = properties.getProperty("password");
        String driver = properties.getProperty("driver");
        String url = properties.getProperty("url");

        //注册驱动
        Class.forName(driver);
        //得到连接
        Connection connection = DriverManager.getConnection(url, user, passwd);
        //组织sql语句
        String sql = "insert into admin values(?, ?)";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,"");
        preparedStatement.setString(2,"");

        ResultSet resultSet = preparedStatement.executeQuery("select * from admin");
        while (resultSet.next()){
            String name = resultSet.getString("name");
            String pwd = resultSet.getString("pwd");
            System.out.println("name " + name + "\tpwd " + pwd);
        }
        //关闭连接
        preparedStatement.close();
        connection.close();

    }
}
